package com.xyht.sca_s.student_manage_system.modules.rxRegistrationWelcome.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationAdvance.entity.RxRegistrationAdvanceStudentInfo;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationAdvance.mapper.RxRegistrationAdvanceStudentInfoMapper;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationWelcome.entity.RxRegistrationWelcomeDelay;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationWelcome.entity.RxRegistrationWelcomeReduction;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationWelcome.entity.req.RxRegistrationWelcomeDelayAllowReq;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationWelcome.entity.req.RxRegistrationWelcomeDelayReq;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationWelcome.entity.resp.RxRegistrationWelcomeDelayResp;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationWelcome.mapper.RxRegistrationWelcomeDelayMapper;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationWelcome.mapper.RxRegistrationWelcomeReductionMapper;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationWelcome.service.RxRegistrationWelcomeDelayService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUser;
import com.xyht.sca_s.student_manage_system.modules.user.entity.resp.SmsUserResp;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.rxRegistrationWelcome.constant.AllowStatusConstant.*;
import static com.xyht.sca_s.student_manage_system.modules.rxRegistrationWelcome.constant.ApplyConstant.*;

/**
 * <p>
 * 招生管理学生缓交申请表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2024-05-15
 */
@Service
public class RxRegistrationWelcomeDelayServiceImpl extends ServiceImpl<RxRegistrationWelcomeDelayMapper, RxRegistrationWelcomeDelay> implements RxRegistrationWelcomeDelayService {

    @Resource
    private RxRegistrationWelcomeDelayMapper rxRegistrationWelcomeDelayMapper;
    @Resource
    private RxRegistrationAdvanceStudentInfoMapper rxRegistrationAdvanceStudentInfoMapper;
    @Resource
    private SmsUserMapper smsUserMapper;
    @Resource
    private RxRegistrationWelcomeReductionMapper rxRegistrationWelcomeReductionMapper;


    @Override
    @Transactional
    public ResponseResult addRegistrationWelcomeDelay(RxRegistrationWelcomeDelayReq addReq) {

        if (isNullOrEmpty(addReq.getStuId(), addReq.getName(), addReq.getIdCard(), addReq.getApplicationReason())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        //缓交和缓交不允许同时提交
        List<RxRegistrationWelcomeReduction> welcomeReductionList = rxRegistrationWelcomeReductionMapper.selectList(new LambdaQueryWrapper<RxRegistrationWelcomeReduction>()
                .eq(RxRegistrationWelcomeReduction::getStuId, addReq.getStuId()));
        if (welcomeReductionList != null && welcomeReductionList.size() > 0) {
            return CommonResult.failed(CommonCodeEnum.RX_REGISTRATION_WELCOME_REDUCE_AND_DELAY_NOT_SUBMIT);
        }
        List<RxRegistrationWelcomeDelay> delayList = rxRegistrationWelcomeDelayMapper.selectList(new LambdaQueryWrapper<RxRegistrationWelcomeDelay>()
                .eq(RxRegistrationWelcomeDelay::getStuId, addReq.getStuId()));
        //校验申请情况
        if (delayList != null && delayList.size() > 0) {
            List<Integer> allowStatusList = delayList.stream().map(RxRegistrationWelcomeDelay::getAllowStatus).collect(Collectors.toList());
            //学生提交申请 通过后不允许提交
            if (allowStatusList.contains(ALLOW_STATUS_PASS)) {
                return CommonResult.failed(CommonCodeEnum.RX_REGISTRATION_WELCOME_APPLY_EXIT_PASS);
            }
            //校验学生有待审批申请下 不可再次提交
            if (allowStatusList.contains(ALLOW_STATUS_WAIT_ALLOW)) {
                return CommonResult.failed(CommonCodeEnum.RX_REGISTRATION_WELCOME_EXIT_ALLOW);
            }
        }

        //校验学生
        String stuId = addReq.getStuId();
        RxRegistrationAdvanceStudentInfo studentInfo = rxRegistrationAdvanceStudentInfoMapper.selectById(stuId);
        if (isNullOrEmpty(studentInfo)) {
            return CommonResult.failed(CommonCodeEnum.RX_REGISTRATION_ADVANCE_STUDENT_NOT_EXIT);
        }
        studentInfo.setDelayStatus(APPLY_STATUS_WAIT_ALLOW);
        rxRegistrationAdvanceStudentInfoMapper.updateById(studentInfo);
        //创建申请表
        RxRegistrationWelcomeDelay rxRegistrationWelcomeDelay = new RxRegistrationWelcomeDelay();
        BeanUtils.copyProperties(addReq, rxRegistrationWelcomeDelay);
        rxRegistrationWelcomeDelay.setAllowStatus(ALLOW_STATUS_WAIT_ALLOW);
        rxRegistrationWelcomeDelayMapper.insert(rxRegistrationWelcomeDelay);

        return CommonResult.success();
    }

    @Override
    public ResponseResult getRegistrationWelcomeDelayList(Integer pageNum, Integer pageSize, String stuName, String idCard, Integer allowStatus) {
        Page<RxRegistrationWelcomeDelay> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<RxRegistrationWelcomeDelay> lambdaQueryWrapper = new LambdaQueryWrapper<RxRegistrationWelcomeDelay>()
                .orderByDesc(RxRegistrationWelcomeDelay::getCreateTime);
        if (!isNullOrEmpty(stuName)) {
            lambdaQueryWrapper.like(RxRegistrationWelcomeDelay::getName, stuName);
        }

        if (!isNullOrEmpty(idCard)) {
            lambdaQueryWrapper.like(RxRegistrationWelcomeDelay::getIdCard, idCard);
        }

        if (!isNullOrEmpty(allowStatus)) {
            lambdaQueryWrapper.eq(RxRegistrationWelcomeDelay::getAllowStatus, allowStatus);
        }

        rxRegistrationWelcomeDelayMapper.selectPage(page, lambdaQueryWrapper);
        List<RxRegistrationWelcomeDelayResp> list = page.getRecords()
                .stream()
                .map(rxRegistrationWelcomeDelay -> {
                    RxRegistrationWelcomeDelayResp resp = new RxRegistrationWelcomeDelayResp();
                    BeanUtils.copyProperties(rxRegistrationWelcomeDelay, resp);
                    return resp;
                })
                .collect(Collectors.toList());
        return CommonResult.success(list,(int)page.getTotal());
    }

    @Override
    public ResponseResult getRegistrationWelcomeDelayDetail(String applyId) {
        if (isNullOrEmpty(applyId)) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        RxRegistrationWelcomeDelay rxRegistrationWelcomeDelay = rxRegistrationWelcomeDelayMapper.selectById(applyId);
        if (isNullOrEmpty(rxRegistrationWelcomeDelay)) {
            return CommonResult.failed(CommonCodeEnum.RX_REGISTRATION_WELCOME_DELAY_NOT_EXIT);
        }
        RxRegistrationWelcomeDelayResp resp = new RxRegistrationWelcomeDelayResp();
        BeanUtils.copyProperties(rxRegistrationWelcomeDelay, resp);
        if (!isNullOrEmpty(rxRegistrationWelcomeDelay.getAllowUserId())) {
            SmsUser smsUser = smsUserMapper.selectById(rxRegistrationWelcomeDelay.getAllowUserId());
            if (!isNullOrEmpty(smsUser)) {
                SmsUserResp allowUserInfo = new SmsUserResp();
                BeanUtils.copyProperties(smsUser, allowUserInfo);
                resp.setAllowUserInfo(allowUserInfo);
            }
        }
        return CommonResult.success(resp);
    }

    @Override
    @Transactional
    public ResponseResult allowRegistrationWelcomeDelayList(RxRegistrationWelcomeDelayAllowReq allowReq, String userId) {
        if (isNullOrEmpty(allowReq.getApplyId(),allowReq.getAllowStatus())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        RxRegistrationWelcomeDelay rxRegistrationWelcomeDelay = rxRegistrationWelcomeDelayMapper.selectById(allowReq.getApplyId());
        if (isNullOrEmpty(rxRegistrationWelcomeDelay)) {
            return CommonResult.failed(CommonCodeEnum.RX_REGISTRATION_WELCOME_DELAY_NOT_EXIT);
        }

        SmsUser smsUser = smsUserMapper.selectById(userId);
        if (isNullOrEmpty(smsUser)) {
            return CommonResult.failed(CommonCodeEnum.USER_NOT_EXIST);
        }
        //审批状态只能是通过和驳回
        Integer allowStatus = allowReq.getAllowStatus();
        if (allowStatus != ALLOW_STATUS_PASS && allowStatus != ALLOW_STATUS_DENY) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        rxRegistrationWelcomeDelay.setAllowStatus(allowStatus);
        rxRegistrationWelcomeDelay.setAllowUserId(userId);
        rxRegistrationWelcomeDelay.setAllowTime(new Date());
        rxRegistrationWelcomeDelay.setAllowOption(allowReq.getAllowOption());
        rxRegistrationWelcomeDelayMapper.updateById(rxRegistrationWelcomeDelay);

        RxRegistrationAdvanceStudentInfo studentInfo = rxRegistrationAdvanceStudentInfoMapper.selectById(rxRegistrationWelcomeDelay.getStuId());
        if (isNullOrEmpty(studentInfo)) {
            return CommonResult.failed(CommonCodeEnum.RX_REGISTRATION_ADVANCE_STUDENT_NOT_EXIT);
        }
        if (allowStatus == ALLOW_STATUS_PASS) {
            studentInfo.setDelayStatus(APPLY_STATUS_PASS);
        } else {
            studentInfo.setDelayStatus(APPLY_STATUS_DENY);
        }
        rxRegistrationAdvanceStudentInfoMapper.updateById(studentInfo);
        return CommonResult.success();
    }

    @Override
    public ResponseResult getMyApply(String stuId) {
        //校验学生
        RxRegistrationAdvanceStudentInfo studentInfo = rxRegistrationAdvanceStudentInfoMapper.selectById(stuId);
        if (isNullOrEmpty(studentInfo)) {
            return CommonResult.failed(CommonCodeEnum.RX_REGISTRATION_ADVANCE_STUDENT_NOT_EXIT);
        }
        List<RxRegistrationWelcomeDelayResp> list = rxRegistrationWelcomeDelayMapper.selectList(new LambdaQueryWrapper<RxRegistrationWelcomeDelay>()
                .eq(RxRegistrationWelcomeDelay::getStuId, stuId)
                .orderByDesc(RxRegistrationWelcomeDelay::getCreateTime))
                .stream()
                .map(rxRegistrationWelcomeDelay -> {
                    RxRegistrationWelcomeDelayResp resp = new RxRegistrationWelcomeDelayResp();
                    BeanUtils.copyProperties(rxRegistrationWelcomeDelay, resp);
                    return resp;
                })
                .collect(Collectors.toList());
        return CommonResult.success(list);
    }

    @Override
    @Transactional
    public ResponseResult cancelRegistrationWelcomeDelay(RxRegistrationWelcomeDelayReq cancelReq) {
        if (isNullOrEmpty(cancelReq.getApplyId(),cancelReq.getStuId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        RxRegistrationWelcomeDelay rxRegistrationWelcomeDelay = rxRegistrationWelcomeDelayMapper.selectById(cancelReq.getApplyId());
        if (isNullOrEmpty(rxRegistrationWelcomeDelay)) {
            return CommonResult.failed(CommonCodeEnum.RX_REGISTRATION_WELCOME_DELAY_NOT_EXIT);
        }
        rxRegistrationWelcomeDelay.setAllowStatus(ALLOW_STATUS_CANCEL);
        rxRegistrationWelcomeDelayMapper.updateById(rxRegistrationWelcomeDelay);
        //修改学生缓交状态
        RxRegistrationAdvanceStudentInfo studentInfo = rxRegistrationAdvanceStudentInfoMapper.selectById(cancelReq.getStuId());
        if (isNullOrEmpty(studentInfo)) {
            return CommonResult.failed(CommonCodeEnum.RX_REGISTRATION_ADVANCE_STUDENT_NOT_EXIT);
        }
        studentInfo.setDelayStatus(APPLY_STATUS_CANCEL);
        rxRegistrationAdvanceStudentInfoMapper.updateById(studentInfo);
        return CommonResult.success();
    }
}
